package com.qiaofang.applet.rest;

import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.dto.DataRequest;
import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.common.dto.PageDataRequest;
import com.qiaofang.applet.common.util.PageUtil;
import com.qiaofang.applet.constants.BusinessConstants;
import com.qiaofang.applet.constants.UpDownFlagConstants;
import com.qiaofang.applet.service.member.domain.Employee;
import com.qiaofang.applet.service.member.service.EmployeePropertyService;
import com.qiaofang.applet.service.member.service.EmployeeService;
import com.qiaofang.applet.service.member.service.dto.EmployeeHouseNumDto;
import com.qiaofang.applet.service.member.service.dto.EmployeeListDto;
import com.qiaofang.applet.service.member.service.dto.EmployeePropertyQueryDto;
import com.qiaofang.applet.service.property.domain.FirsthandEstate;
import com.qiaofang.applet.service.property.domain.Property;
import com.qiaofang.applet.service.property.service.FirsthandEstateService;
import com.qiaofang.applet.service.property.service.PropertyService;
import com.qiaofang.applet.service.property.service.dto.*;
import com.qiaofang.applet.service.property.service.mapstruct.FirsthandEstateMapper;
import com.qiaofang.applet.service.property.service.mapstruct.PropertyMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/pcweb/employee")
@Api(tags = "经纪人：经纪人模块")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private EmployeePropertyService employeePropertyService;

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private FirsthandEstateService firsthandEstateService;

    @Autowired
    private PropertyMapper propertyMapper;

    @Autowired
    private FirsthandEstateMapper firsthandEstateMapper;

    @PostMapping("/getEmployee")
    @ApiOperation(value = "查询经纪人详情", notes = "查询经纪人详情")
    public DataResponse<EmployeeListDto> getEmployee(@RequestBody @Valid DataRequest<UniqueUuidDto> request) {
        Employee employee = employeeService.findByEmployeeUniqueUuid(request.getParam().getUniqueUuid());
        EmployeeListDto result = employeeService.domainToListDto(employee);
        return DataResponse.ok(result);
    }

    @PostMapping("/auth/getEmployeeHouseNum")
    @ApiOperation(value = "查询经纪人房源套数", notes = "查询经纪人房源套数")
    public DataResponse<EmployeeHouseNumDto> getEmployeeHouseNum(@RequestBody @Valid DataRequest<UniqueUuidDto> request) {
        return DataResponse.ok(employeePropertyService.getEmployeeHouseNum(request.getParam().getUniqueUuid()));
    }

    @PostMapping("/searchEmployeeFirsthand")
    @ApiOperation(value = "查询经纪人新房列表", notes = "查询经纪人新房列表")
    public DataResponse<Page<FirsthandEstateListDto>> searchEmployeeFirsthand(@RequestBody @Valid PageDataRequest<FirsthandEstateQueryDto> request) {
        request.getParam().setCityCode(QfContextHolder.getContext().getCityCode());
        EmployeePropertyQueryDto employeePropertyQueryDto = new EmployeePropertyQueryDto();
        employeePropertyQueryDto.setEmployeeUniqueUuid(request.getParam().getUniqueUuid());
        employeePropertyQueryDto.setPropertyType(BusinessConstants.FIRSTHAND_ESTATE.getValue());

        List<String> propertyList = employeePropertyService.findPropertyListByParams(employeePropertyQueryDto);

        Page<FirsthandEstateListDto> result = new PageImpl<>(new ArrayList<>());

        if (!CollectionUtils.isEmpty(propertyList)) {
            request.getParam().setEstateUniqueUuids(propertyList);
            request.getParam().setUpDownFlag(UpDownFlagConstants.UP_FLAG.getValue());

            Page<FirsthandEstate> firsthandEstatePage = firsthandEstateService.findAllByParams(request.getParam(), PageUtil.getPageable(request));

            result = firsthandEstateMapper.convertDomainToListDto(firsthandEstatePage);
        }

        return DataResponse.ok(result);
    }

    @PostMapping("/searchEmployeeSale")
    @ApiOperation(value = "查询经纪人二手房列表", notes = "查询经纪人二手房列表")
    public DataResponse<Page<SalePropertyListDto>> searchEmployeeSale(@RequestBody @Valid PageDataRequest<SalePropertyQueryDto> request) {
        request.getParam().setCityCode(QfContextHolder.getContext().getCityCode());
        EmployeePropertyQueryDto employeePropertyQueryDto = new EmployeePropertyQueryDto();
        employeePropertyQueryDto.setEmployeeUniqueUuid(request.getParam().getUniqueUuid());
        employeePropertyQueryDto.setPropertyType(BusinessConstants.SALE_PROPERTY.getValue());

        List<String> propertyList = employeePropertyService.findPropertyListByParams(employeePropertyQueryDto);

        Page<SalePropertyListDto> result = new PageImpl<>(new ArrayList<>());

        if (!CollectionUtils.isEmpty(propertyList)) {
            request.getParam().setPropertyUniqueUuids(propertyList);
            request.getParam().setUpDownFlag(UpDownFlagConstants.UP_FLAG.getValue());

            Page<Property> propertyPage = propertyService.findAllSaleByParams(request.getParam(), PageUtil.getPageable(request));

            result = propertyMapper.convertDomainToSaleListDto(propertyPage);
        }

        return DataResponse.ok(result);
    }

    @PostMapping("/searchEmployeeRent")
    @ApiOperation(value = "查询经纪人租房列表", notes = "查询经纪人租房列表")
    public DataResponse<Page<RentPropertyListDto>> searchEmployeeRent(@RequestBody @Valid PageDataRequest<RentPropertyQueryDto> request) {
        request.getParam().setCityCode(QfContextHolder.getContext().getCityCode());
        EmployeePropertyQueryDto employeePropertyQueryDto = new EmployeePropertyQueryDto();
        employeePropertyQueryDto.setEmployeeUniqueUuid(request.getParam().getUniqueUuid());
        employeePropertyQueryDto.setPropertyType(BusinessConstants.RENT_PROPERTY.getValue());

        List<String> propertyList = employeePropertyService.findPropertyListByParams(employeePropertyQueryDto);

        Page<RentPropertyListDto> result = new PageImpl<>(new ArrayList<>());

        if (!CollectionUtils.isEmpty(propertyList)) {
            request.getParam().setPropertyUniqueUuids(propertyList);
            request.getParam().setUpDownFlag(UpDownFlagConstants.UP_FLAG.getValue());

            Page<Property> propertyPage = propertyService.findAllRentByParams(request.getParam(), PageUtil.getPageable(request));

            result = propertyMapper.convertDomainToRentListDto(propertyPage);
        }

        return DataResponse.ok(result);
    }
}
